/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * alertaMail.java
 *
 * Created on 26-may-2009, 23:51:25
 */

package alertas;

import empresa.DatosEmpresa;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import laboratorio.Facturas;
import emailSW.MailUtils;
import gestion.Factura;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import javax.persistence.Query;

/**
 *
 * @author Juan Antonio Tirado
 */
public class alertaMail extends javax.swing.JFrame {
    //ATRIBUTOS
    private Query q;
    private Facturas VentanaFacturaOld;
    private Factura factura;


    /** Creates new form alertaMail */
    public alertaMail() {
        initComponents();
    }

    public alertaMail(Facturas facturasOld) {
        initComponents();
        VentanaFacturaOld=facturasOld;

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lpdPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("lpdPU").createEntityManager();
        datosEmpresaQuery = java.beans.Beans.isDesignTime() ? null : lpdPUEntityManager.createQuery("SELECT d FROM DatosEmpresa d");
        datosEmpresaList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : datosEmpresaQuery.getResultList();
        facturaQuery = java.beans.Beans.isDesignTime() ? null : lpdPUEntityManager.createQuery("SELECT f FROM Factura f");
        facturaList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : facturaQuery.getResultList();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(laboratorio.LaboratorioApp.class).getContext().getResourceMap(alertaMail.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                enviarEmail(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                salir(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addComponent(jLabel1)
                .addContainerGap(46, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(177, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(41, 41, 41)
                .addComponent(jButton2)
                .addGap(36, 36, 36))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(67, 67, 67)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap(38, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void salir(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_salir
        this.setVisible(false);
    }//GEN-LAST:event_salir

    private void enviarEmail(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_enviarEmail
        String tit = null;
        try {
            // TODO add your handling code here:
            q = lpdPUEntityManager.createQuery("SELECT d FROM DatosEmpresa d");
            List aa = q.getResultList();
            DatosEmpresa datosempresa = (DatosEmpresa) aa.get(0);
            String de = datosempresa.getEmail();
            factura = VentanaFacturaOld.CogeFactura();
            String asunto = "Factura ".concat(factura.getFecha()).concat(" con numero de factura : ").concat(String.valueOf(factura.getIDfactura()));
            String contenido = "Pasamos a enviarle la factura antes indicada";
            //String para=cliente.getEmail();
            tit = "factura_".concat(String.valueOf(factura.getIDfactura())).concat("_cliente_").concat(String.valueOf(factura.getIDcliente())).concat("_fecha_").concat(factura.getFecha()).concat(".pdf");
            String para = "tiradogonzalezveronica@gmail.com";
            String archivo = "C:\\Users\\Juan Antonio Tirado\\Documents\\NetBeansProjects\\trunk\\laboratorio\\".concat(tit); //"/home/rocio/NetBeansProjects/trunk/trunk/laboratorio".concat(tit);
            MailUtils j = new MailUtils();
            j.sendMail(asunto, contenido, de, para, archivo);
        } catch (Exception ex) {
            Logger.getLogger(alertaMail.class.getName()).log(Level.SEVERE, null, ex);
        }

        File src=new  File(tit);
        File dst=new File("C:\\Users\\Juan Antonio Tirado\\Documents\\Aptana Studio\\lpd\\pdf\\".concat(tit));
        FileInputStream in = null;
        FileOutputStream  out = null;
        try {
            in = new FileInputStream(src);
            out = new FileOutputStream(dst);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(alertaMail.class.getName()).log(Level.SEVERE, null, ex);
        }

        int c;

        byte[] buf = new byte[1024];
        int len;
        try {
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } catch (IOException ex) {
            Logger.getLogger(alertaMail.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            in.close();
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(alertaMail.class.getName()).log(Level.SEVERE, null, ex);
        }


        this.setVisible(false);

    }//GEN-LAST:event_enviarEmail

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new alertaMail().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.util.List<empresa.DatosEmpresa> datosEmpresaList;
    private javax.persistence.Query datosEmpresaQuery;
    private java.util.List<gestion.Factura> facturaList;
    private javax.persistence.Query facturaQuery;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.persistence.EntityManager lpdPUEntityManager;
    // End of variables declaration//GEN-END:variables

}
